package org.neuedu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import netscape.javascript.JSObject;
import org.neuedu.service.PayService;
import org.neuedu.util.PayConfig;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
@Service
public class PayServiceImpl implements PayService {
    @Override
    public String AliPay(String cardNum, BigDecimal registMoney,String orderName) {
        AlipayClient client= new DefaultAlipayClient
                (PayConfig.GATEWAYURL,PayConfig.APPID,PayConfig.PRIVATEKEY,PayConfig.FORMAT,
                        PayConfig.CHARSET,PayConfig.ALIPAYPUBLICKEY,PayConfig.SIGNTYPE);
        AlipayTradePagePayRequest request=new AlipayTradePagePayRequest();
        request.setReturnUrl("http://127.0.0.1/register");
        request.setNotifyUrl("http://38tab7.natappfree.cc/register");//内网穿透才能用
//        request.setReturnUrl("http://123.56.65.214/register");
//        request.setNotifyUrl("http://123.56.65.214/register");//内网穿透才能用
        final JSONObject jsonObject=new JSONObject();
        jsonObject.put("out_trade_no",cardNum);
        jsonObject.put("total_amount",registMoney.toString());
        jsonObject.put("subject",orderName);
        jsonObject.put("body",orderName);
        jsonObject.put("product_code","FAST_INSTANT_TRADE_PAY");
        request.setBizContent((jsonObject.toJSONString()));
        try {
            return client.pageExecute(request).getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
    }
}
